import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { FreightAccountService } from '../../services/freight-account.service';
import { ClearingModalComponent } from '../abnormal-gold/clearing-modal/clearing-modal.component';

@Component({
  selector: 'app-payment-record',
  templateUrl: './payment-record.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class PaymentRecordComponent extends BasicTableComponent implements OnInit {
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  paymentStatus: any = '';

  constructor(
    public service: FreightAccountService,
    private nzModalService: NzModalService,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value,
        applyDate: {
          start: this.sf?.value.applyDate?.[0] || '',
          end: this.sf?.value.applyDate?.[1] || ''
        },
        handlerDate: {
          start: this.sf?.value.handlerDate?.[0] || '',
          end: this.sf?.value.handlerDate?.[1] || ''
        }
      });
    }
    Object.assign(requestOptions.body, { paymentStatus: this.paymentStatus || null });
    return requestOptions;
  };

  changePaymentStatus(status?: string) {
    this.paymentStatus = status || null;
    this.st.load(1);
  }

  refund(item: any) {
    this.nzModalService.warning({
      nzTitle: '确定要关闭所选支付记录？',
      nzOnOk: () => {}
    });
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        orderPaymentCode: {
          type: 'string',
          title: '支付编号',
          ui: {
            placeholder: '请输入'
          }
        },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        resourceCode: {
          type: 'string',
          title: '货源编号',
          ui: {
            placeholder: '请输入'
          }
        },
        serviceType: {
          type: 'string',
          title: '服务类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'service:type' },
            placeholder: '请选择'
          },
          default: ''
        },
        driver: {
          type: 'string',
          title: '承运司机',
          ui: {
            placeholder: '请输入'
          }
        },
        driverLicensePlate: {
          type: 'string',
          title: '车牌号',
          ui: {
            placeholder: '请输入'
          }
        },
        captain: {
          type: 'string',
          title: '收款人',
          ui: {
            placeholder: '请输入收款人姓名/手机号'
          }
        },
        isCaptain: {
          type: 'string',
          title: '车队长收款',
          enum: [
            { label: '全部', value: '' },
            { label: '是', value: '1' },
            { label: '否', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        payType: {
          type: 'string',
          title: '支付类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'paybill:type' },
            placeholder: '请选择'
          },
          default: ''
        },
        applyDate: {
          title: '申请时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true
          } as SFDateWidgetSchema
        },
        handlerDate: {
          title: '处理时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true
          } as SFDateWidgetSchema
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: '' },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '支付编号', render: 'orderPaymentCode', width: 200 },
      {
        title: '支付金额',
        render: 'payAmount',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.payAmount }) }
      },
      {
        title: '运费明细',
        render: 'amountDetails',
        width: 160
        // format: item => {
        //   (item.amountDetails as Array<any>).forEach(detail => {
        //     detail.surcharge = detail.price + (detail.surcharge || 0);
        //   });
        //   console.log(item.amountDetails);

        //   return '';
        // }
      },
      {
        title: '支付类型',
        index: 'payTypeLabel',
        width: 140
      },
      { title: '货主', index: 'enterpriseInfoName', width: 180 },
      { title: '订单号', render: 'billCode', width: 170 },
      { title: '运单号', render: 'wayBillCode', width: 170 },
      { title: '货源编号', index: 'resourceCode', width: 160 },
      {
        title: '服务类型',
        index: 'serviceTypeLabel',
        width: 150
      },
      { title: '承运司机', render: 'driverId', width: 140 },
      { title: '收款人', render: 'captainName', width: 140 },
      { title: '银行类型', index: 'bankType', width: 150, type: 'enum', enum: { 1: '平安银行', 2: '浦发银行' } },
      { title: '网络货运人', index: 'ltdName', width: 160 },
      { title: '申请时间', index: 'applyTime', width: 160 },
      { title: '申请人', index: 'applyUserName', width: 90 },
      { title: '处理时间', index: 'handlerTime', width: 180 },
      { title: '处理人', index: 'handlerUserName', width: 90 },
      { title: '退款状态', index: 'refundStatusLabel', width: 220 },
      { title: '备注', index: 'failCause', width: 250 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '关闭',
            iif: item => item.paymentStatus === '4',
            click: item => this.refund(item)
          }
        ]
      }
    ];
  }
  // 导出
  exprot() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_asyncExportBillPaymentApplicationList);
  }
}
